// pages/favorite/favorite.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allChecked: false,
    hiddenEdit: true, //是否弹出编辑
    showList1: true,
    titleList1: '全部类型',
    showList2: true,
    modelType: '', //存放机型信息

    titleList2: '',
    imgList1: '../../images/downImg.png',
    imgList2: '../../images/downImg.png',
    typeList1: {
      showList1: true,
      titleList1: '全部类型',
      child: [{
        title: '全部类型',
        selected: true
      }, {
        title: '手机',
        selected: false
      }, {
        title: '电脑周边',
        selected: false
      }, {
        title: '五金/工具',
        selected: false
      }]
    },
    typeList2: {
      showList2: true,
      titleList2: '全部状态',
      child: [{
          title: '全部状态',
          selected: true
        },
        {
          title: '降价中',
          selected: false
        }, {
          title: '低库存',
          selected: false
        }, {
          title: '失效',
          selected: false
        }
      ]
    },
    cartsData1: [{
        goodsName: '店铺2的商品而过微微大概的个人各设若干和二手房哥和他认识',
        speci: '规格1',
        price: '266.30',
        num: 3,
        selected: false,
      },
      {
        goodsName: '店铺1的商品1',
        speci: '规格1',
        price: '128.30',
        num: 8,
        selected: false,
      },
      {
        goodsName: '店铺1的商品2',
        speci: '规格2',
        price: '531.89',
        num: 4,
        selected: false,
      },
      {
        goodsName: '店铺1的商品2',
        speci: '规格2',
        price: '531.89',
        num: 4,
        selected: false,
      },
      {
        goodsName: '店铺1的商品2',
        speci: '规格2',
        price: '531.89',
        num: 4,
        selected: false,
      },
    ],
  },
  goodsCheck(e) { //单项选择
    console.log(e)
    this.setData({
      ["cartsData1[" + e.currentTarget.dataset.id + "].selected"]: !this.data.cartsData1[e.currentTarget.dataset.id].selected
    })

    let dataLength = this.data.cartsData1.length
    let thisLength = 0
    for (let i in this.data.cartsData1) {
      if (this.data.cartsData1[i].selected == true) {
        thisLength++
      }
    }

    if (dataLength == thisLength) {
      this.setData({
        allChecked: true
      })
    } else {
      this.setData({
        allChecked: false
      })
    }
  },
  allChange(e) { //全选
    this.setData({
      allChecked: e.detail
    })
    for (let i in this.data.cartsData1) {
      var fatherSeleted = "cartsData1[" + i + "].selected"
      this.setData({
        [fatherSeleted]: this.data.allChecked
      })
    }
  },
  hiddenItem(e) { //点击屏幕隐藏菜单
    // console.log(e)
    this.setData({
      ['typeList1.showList1']: true,
      ['typeList2.showList2']: true,
      imgList1: '../../images/downImg.png',
      imgList2: '../../images/downImg.png',
    })
  },
  checkType(e) { //下拉菜单
    // console.log(e.currentTarget.dataset.type)
    if (e.currentTarget.dataset.type == 1) { //区分选项
      this.setData({
        ['typeList1.showList1']: !this.data.typeList1.showList1
      })
      // console.log(this.data.typeList1.showList1)
      if (this.data.typeList1.showList1 == true) {
        this.setData({
          imgList1: '../../images/downImg.png',
          imgList2: '../../images/downImg.png',
        })
      } else {
        this.setData({
          imgList1: '../../images/upImg.png',
          ['typeList2.showList2']: true,
          imgList2: '../../images/downImg.png',
        })
      }
    } else {
      this.setData({
        ['typeList2.showList2']: !this.data.typeList2.showList2
      })
      // console.log(this.data.typeList1.showList1)
      if (this.data.typeList2.showList2 == true) {
        this.setData({
          imgList2: '../../images/downImg.png',
          imgList1: '../../images/downImg.png',
        })
      } else {
        this.setData({
          imgList2: '../../images/upImg.png',
          ['typeList1.showList1']: true,
          imgList1: '../../images/downImg.png',
        })
      }
    }

  },
  checkItem(e) { //选择菜单项
    // console.log(e.currentTarget.dataset.type)
    if (e.currentTarget.dataset.type == 1) { //区分选项
      for (let i in this.data.typeList1.child) {

        // console.log(i)
        this.setData({
          ['typeList1.child[' + i + '].selected']: false,
          ['typeList1.child[' + e.currentTarget.dataset.id + '].selected']: true,
          ['typeList1.titleList1']: this.data.typeList1.child[e.currentTarget.dataset.id].title,
          ['typeList1.showList1']: true
        })

        this.setData({
          imgList1: '../../images/downImg.png',
          imgList2: '../../images/downImg.png'
        })
      }
    } else {
      for (let i in this.data.typeList2.child) {

        // console.log(i)
        this.setData({
          ['typeList2.child[' + i + '].selected']: false,
          ['typeList2.child[' + e.currentTarget.dataset.id + '].selected']: true,
          ['typeList2.titleList2']: this.data.typeList2.child[e.currentTarget.dataset.id].title,
          ['typeList2.showList2']: true
        })

        this.setData({
          imgList2: '../../images/downImg.png',
          imgList1: '../../images/downImg.png'
        })
      }
    }

  },
  changeEdit(e) { //弹出编辑
    // console.log(e)
    this.setData({
      hiddenEdit: !this.data.hiddenEdit,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.imputedPrice()
    // console.log(this.data.typeList1.titleList1)

    let that = this
    wx.getSystemInfo({ //机型判断
      success: function(res) {
        if (res.model.search('iPhone X') > -1) { //判断当前机型是否是iiPhone X系列（底部留出空白）
          that.setData({
            modelType: res.model
          })
        }
        console.log(that.data.modelType)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})